package com.ddddddl.zhangben.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.ddddddl.zhangben.R;
import com.ddddddl.zhangben.data.Type;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TypeAdapter extends BaseAdapter {
	
	private Context mContext;
	private ArrayList<Type> mTypes = new ArrayList<Type>();
	private HashMap<Long, Integer> mPositionMap = new HashMap<Long, Integer>();
	public static final int NOT_SELECTED_POSITION = -1;
	private int mSelectedPosition = NOT_SELECTED_POSITION;
	
	public TypeAdapter(Context context) {
		mContext = context;
	}

	@Override
	public int getCount() {
		return mTypes.size();
	}

	@Override
	public Type getItem(int position) {
		return mTypes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mTypes.get(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.type_item, null);
		}
		TextView name = (TextView) convertView.findViewById(R.id.name);
		Type type = mTypes.get(position);
		name.setText(type.name);
		
		Button updateFirstType = (Button) convertView.findViewById(R.id.update_first_type);
		updateFirstType.setOnClickListener((OnClickListener) mContext);
		Button updateSecondType = (Button) convertView.findViewById(R.id.update_second_type);
		updateSecondType.setOnClickListener((OnClickListener) mContext);
		if (position == mSelectedPosition) {
			name.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
			name.setTextColor(mContext.getResources().getColor(R.color.white));
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
			
			if (type.pID == Type.NO_PID) {
				updateFirstType.setVisibility(View.VISIBLE);
				updateSecondType.setVisibility(View.GONE);
			} else {
				updateFirstType.setVisibility(View.GONE);
				updateSecondType.setVisibility(View.VISIBLE);
			}
		} else {
			name.setBackgroundColor(mContext.getResources().getColor(R.color.white));
			name.setTextColor(mContext.getResources().getColor(R.color.blue));
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
			
			updateFirstType.setVisibility(View.GONE);
			updateSecondType.setVisibility(View.GONE);
		}
		return convertView;
	}
	
	public void setData(ArrayList<Type> types) {
		if (types == null) {
			types = new ArrayList<Type>();
		}
		mTypes = types;
		for (int i = 0; i < types.size(); i++) {
			mPositionMap.put(types.get(i).id, i);
		}
		notifyDataSetChanged();
	}
	
	public void setSelectedPosition(int position) {
		mSelectedPosition = position;
		notifyDataSetChanged();
	}
	
	public long getSelectedItemId() {
		if (mSelectedPosition < 0) {
			return mSelectedPosition;
		}
		return getItemId(mSelectedPosition);
	}
	
	public Type getSelectedItem() {
		if (mSelectedPosition < 0) {
			return null;
		}
		return getItem(mSelectedPosition);
	}
	
	public void setSelectedItem(long id) {
		Integer position = mPositionMap.get(id);
		if (position == null) {
			mSelectedPosition = -1;
		} else {
			mSelectedPosition = position;
		}
		notifyDataSetChanged();
	}

	public void addData(Type type) {
		mTypes.add(type);
		mPositionMap.put(type.id, mTypes.size() - 1);
		notifyDataSetChanged();
	}

	public void updateType(Type type) {
		int position = mPositionMap.get(type.id);
		mTypes.set(position, type);
		notifyDataSetChanged();
	}

}
